package com.ruoyi.system.util;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Component
public class FileUtil {
    public static String savePhoto(MultipartFile photo) throws IOException {
        // 生成唯一文件名
        String originalFileName = photo.getOriginalFilename();
        String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + fileExtension;

        // 保存文件到指定目录
        String filePath = RuoYiConfig.getUploadPath() + newFileName;
        File dest = new File(filePath);
        photo.transferTo(dest);

        return newFileName;
    }
}
